using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

namespace Framework
{

    [CustomEditor(typeof(UGUIWidget))]
    public class UGUIWidgetEditor : Editor
    {
        private List<IUGUIEditorProcessor> _processors;
        private UGUIWidget _target;
        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            OnDraw();
            serializedObject.ApplyModifiedProperties();
        }
        protected virtual void OnDraw()
        {
            if (EditorApplication.isPlaying)
            {
                //return;
            }
            if (EditorApplication.isCompiling || EditorApplication.isUpdating)
            {
                EditorGUILayout.HelpBox("Wiatting for compiling or updating...", MessageType.Warning);
                return;
            }

            EditorGUI.BeginDisabledGroup(true);
            SerializedProperty bindClass = serializedObject.FindProperty("bindClass");
            EditorGUILayout.PropertyField(bindClass, new GUIContent("绑定类"));
            EditorGUI.EndDisabledGroup();




            SerializedProperty codePath = serializedObject.FindProperty("codePath");
            if (string.IsNullOrEmpty(codePath.stringValue))
            {
                codePath.stringValue = target.name;
            }
            EditorGUILayout.Space();
            EditorGUILayout.BeginHorizontal();

            EditorGUI.BeginDisabledGroup(EditorApplication.isPlaying);
            EditorGUILayout.PropertyField(codePath, new GUIContent("类名"), GUILayout.MinWidth(40));
            var mButtonstyle = new GUIStyle(GUI.skin.button);
            if (GUILayout.Button("自动类名", mButtonstyle))
            {
                bool change = false;
                string newbindClass = Utils.Text.FirstCharUpper(_target.name.Replace(" ", ""));
                var processors = GetProcessor();
                if (processors.Count > 0)
                {
                    newbindClass = processors[0].GetBindClassPath(newbindClass);
                }
                if (!string.IsNullOrEmpty(bindClass.stringValue) && bindClass.stringValue != newbindClass)
                {
                    if (EditorUtility.DisplayDialog("警告", "确定要更改吗?", "确定", "取消"))
                    {
                        change = true;
                    }
                }
                else
                {
                    change = true;
                }
                if (change)
                {
                    bindClass.stringValue = newbindClass;
                }
            }
            EditorGUI.EndDisabledGroup();
            EditorGUILayout.EndHorizontal();


            mButtonstyle.fixedHeight = 30f;
            EditorGUILayout.BeginHorizontal();
            EditorGUI.BeginDisabledGroup(EditorApplication.isPlaying);
            if (GUILayout.Button("生成代码", mButtonstyle))
            {
                var processors = GetProcessor();
                if (processors.Count < 1)
                {
                    EditorUtility.DisplayDialog("提示", "请实现 " + typeof(IUGUIEditorProcessor).ToString() + " 接口", "确定");
                }
                else
                {
                    processors[0].CreateCodeFile(_target);
                }
            }
            EditorGUI.EndDisabledGroup();
            if (GUILayout.Button("编辑代码", mButtonstyle))
            {
                var processors = GetProcessor();
                if (processors.Count < 1)
                {
                    EditorUtility.DisplayDialog("提示", "请实现 " + typeof(IUGUIEditorProcessor).ToString() + " 接口", "确定");
                }
                else
                {
                    processors[0].OpenCodeFile(_target);
                    // InternalEditorUtility.OpenFileAtLineExternal(filePath, 0);
                }
            }
            EditorGUILayout.EndHorizontal();
        }
        protected virtual void OnEnable()
        {
            _target = target as UGUIWidget;
        }
        protected List<IUGUIEditorProcessor> GetProcessor()
        {
            if (_processors == null)
            {
                _processors = new List<IUGUIEditorProcessor>();

                var typelist = Utils.Assembly.GetEditorTypes(typeof(IUGUIEditorProcessor));
                foreach (var item in typelist)
                {

                    if (System.Activator.CreateInstance(item) is IUGUIEditorProcessor cmd)
                    {
                        _processors.Add(cmd);
                    }
                }
                if (_processors.Count > 0)
                {
                    if (_processors.Count > 1)
                    {
                        Debug.LogError("存在多个处理接口,请保留一个");
                    }
                }
            }

            return _processors;
        }
    }
}